package com.siwei.mes.service.mixProportion.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.mixproportion.MixProportionVerifyRecord;
import com.siwei.mes.mapper.mixProportion.MixProportionMapper;
import com.siwei.mes.mapper.mixProportion.MixProportionVerifyRecordMapper;
import com.siwei.mes.mapper.syncErp.ErpProjectMapper;
import com.siwei.mes.service.mixProportion.MixProportionVerifyRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 配合比验证记录(MixProportionVerifyRecord)表服务实现类
 *
 * @author linzi
 * @since 2024-10-23 10:23:21
 */
@Service
public class MixProportionVerifyRecordServiceImpl implements MixProportionVerifyRecordService {
    @Resource
    private MixProportionVerifyRecordMapper mixProportionVerifyRecordMapper;
    @Resource
    private MixProportionMapper mixProportionMapper;
    @Resource
    private ErpProjectMapper erpProjectMapper;

    @Override
    public Long create(MixProportionVerifyRecord entity) {

        assertTrue(mixProportionMapper.loadById(entity.getMixId()) == null, "配合比信息不存在");
        assertTrue(erpProjectMapper.loadById(entity.getProjectId()) == null, "工程信息不存在");

        /**判断是是否存在记录   配合比id+工程id*/
        MixProportionVerifyRecord exist = this.isExist(entity.getMixId(), entity.getProjectId());
        assertTrue(exist != null, "该配合比已存在此工程的验证记录");

        entity.init(true);
        mixProportionVerifyRecordMapper.insertSelective(entity);
        return entity.getId();
    }

    /**
     * 判断是否存在记录
     *
     * @param mixId     配合比id
     * @param projectId 工程id
     * @return
     */
    private MixProportionVerifyRecord isExist(Long mixId, Long projectId) {
        MixProportionVerifyRecord select = new MixProportionVerifyRecord();
        select.setMixId(mixId).setProjectId(projectId);
        return mixProportionVerifyRecordMapper.selectOne(select);
    }

    @Override
    public void update(MixProportionVerifyRecord entity) {
        MixProportionVerifyRecord mixProportionVerifyRecord = mixProportionVerifyRecordMapper.selectByPrimaryKey(entity.getId());
        assertTrue(mixProportionVerifyRecord == null, "配合比验证记录信息不存在");
        assertTrue(mixProportionMapper.loadById(entity.getMixId()) == null, "配合比信息不存在");
        assertTrue(erpProjectMapper.loadById(entity.getProjectId()) == null, "工程信息不存在");
        entity.init(false);
        if (!mixProportionVerifyRecord.getMixId().equals(entity.getMixId()) || !mixProportionVerifyRecord.getProjectId().equals(entity.getProjectId())) {
            /**判断是是否存在记录   配合比id+工程id*/
            MixProportionVerifyRecord exist = this.isExist(entity.getMixId(), entity.getProjectId());
            assertTrue(exist != null, "该配合比已存在此工程的验证记录");
        }
        mixProportionVerifyRecordMapper.updateByPrimaryKey(entity);
    }

    @Override
    public void delete(Long id) {
        MixProportionVerifyRecord mixProportionVerifyRecord = mixProportionVerifyRecordMapper.selectByPrimaryKey(id);
        assertTrue(mixProportionVerifyRecord == null, "配合比验证记录信息不存在");
        mixProportionVerifyRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<MixProportionVerifyRecord> getPage(PageQuery<MixProportionVerifyRecord, MixProportionVerifyRecord> query) {
        MixProportionVerifyRecord searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> mixProportionVerifyRecordMapper.getList(searchParam));
    }

    @Override
    public MixProportionVerifyRecord loadById(Long id) {
        return mixProportionVerifyRecordMapper.loadById(id);
    }

    @Override
    public List<MixProportionVerifyRecord> getAll(MixProportionVerifyRecord entity) {
        return mixProportionVerifyRecordMapper.getList(entity);
    }
}
